const express = require('express');
const router = express.Router();
const query = require('../query/pool');
const token = require('../token/token');
router.use(express.json());
router.use(express.urlencoded());

// 登录
router.post('/loading',(req,res)=>{
    let {user,pwd} = req.body;
    let sql = 'SELECT * FROM `user` WHERE user=? and pwd=?';
    query(sql,data=>{
        console.log(data);
        if(data.length>0){
            let obj = {
                code:200,
                msg:'登录成功',
                data:data[0],
                token:token.set({user,pwd},'36h')
            }
            res.send(obj);
        }else{
            let obj = {
                code:0,
                msg:'登录失败',
            }
            res.send(obj);
        }
    },[user,pwd]);
});

//查询所有地址
router.get('/getAddress',(req,res)=>{
    query('SELECT * FROM `address`',data=>{
        res.send(data);
    });
});

//提交数据
router.post('/add',(req,res)=>{
    let { name,area,address,tel,tips,bol } = req.body;
    let sql = 'INSERT INTO `address`( `name`, `area`, `address`, `tel`, `tips`, `bol`) VALUES (?,?,?,?,?,?)';
    if(bol=='0'){
        let sql1 = 'UPDATE `address` SET `bol`=1';
        query(sql1,data=>{
            console.log(data);
        });
        query(sql,data=>{
            console.log(data);
            let obj = {
                msg:'添加成功',
                token:token.set({name},'1h')
            };
            res.send(obj);
        },[name,area,address,tel,tips,bol]);
    }else{
        query(sql,data=>{
            console.log(data);
            let obj = {
                msg:'添加成功',
                token:token.set({name},'1h')
            };
            res.send(obj);
        },[name,area,address,tel,tips,bol]);
    }
    
});

//删除
router.post('/del',(req,res)=>{
    let { id } = req.body;
    let sql = 'DELETE FROM `address` WHERE id=?';
    query(sql,data=>{
        console.log(data);
        let obj = {
            msg:'删除成功',
        };
        res.send(obj);
    },[id]);
});

//设为默认
router.post('/edit',(req,res)=>{
    let { id } = req.body;
    let sql = 'UPDATE `address` SET `bol`=1 WHERE id=?';
    let sql1 = 'UPDATE `address` SET `bol`=0';
    query(sql1,data=>{
        console.log(data);
    });
    query(sql,data=>{
        console.log(data);
        let obj = {
            msg:'设为默认地址成功',
        };
        res.send(obj);
    },[id]);

});

// 修改地址
router.get('/editAddress/:id',(req,res)=>{
    let { id } = req.params;
    console.log(id);
    let sql = 'SELECT `name`, `area`, `address`, `tel`, `tips`, `bol` FROM `address` WHERE id=?';
    query(sql,data=>{
        let obj = {
            code:200,
            msg:'查找成功',
            data:data
        }
        res.send(obj);
    },[id]);
});

router.post('/saveAddress',(req,res)=>{
    let { name,area,address,tel,tips,bol,id } = req.body;
    let sql ='UPDATE `address` SET `name`=?,`area`=?,`address`=?,`tel`=?,`tips`=?,`bol`=? WHERE id=?';
    query(sql,data=>{
        let obj ={
            code:200,
            msg:'修改成功',
            data:data
        }
        res.send(obj);
    },[name,area,address,tel,tips,bol,id]);
});
module.exports = router;